<?php
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.

$root_path = "../";

if (isset($_POST["submit_mail"])){
    include_once("../includes/utils.php");


    // first check the recaptcha answer

    if ( $_POST["recaptcha_validation"] != '1' ){
        die("<font color='#9E0000'>"._("The reCAPTCHA was entered <b>incorrectly</b>. Please try again."));
    }

    $name = $_POST["name"];
    $mail = $_POST["email"];
    $phone = $_POST["phone"];
    $message = "
Name: $name
Email: $mail
Phone: $phone
Message:
${_POST['message']}
";
    $key = "FORM123FDG)@#*$@%maq";
    $dest = string_decrypt($_POST["dest"], $key);

    // validate fields
    $valid = 1;
    if (strrpos($mail, "@") == 0) {
        $valid = 0;
        echo "<font color='#9E0000'>"._("Please enter a valid email address").".</font>";
    }

    if ($valid){
        if (mail($dest, _("Message from $site_title"), $message)){;
            echo "<font color='#002A9E'>"._("Message sent succesfully")."</font>";
        } else {
            echo "<font color='#9E0000'>"._("Mail could not be sent, if this error persist you can contact us through $dest").".</font>";
        }
    }


} else {

    function execute_form($site_content){
        $html = str_get_html($site_content);

        foreach($html->find("a") as $element){
            if ($element->class == "ekkin"){
                if ($element->id == "form"){
                    $mail = $element->href;
                    $fields = explode(",", $element->innertext);
                    //print_r($fields);
                    $element -> outertext = generate_form($mail, $fields);
                }

            }
        }


        return $html->save();
    }
}



function generate_form($mail, $fields){
    global $lang;
    $key = "FORM123FDG)@#*$@%maq";

    $mail = string_encrypt($mail, $key);
    $form = "<script type='text/javascript' src='${relpath}ext_lib/webtoolkit.aim.js'></script>
    <script type='text/javascript' src='${relpath}ext_lib/recaptcha.js'></script>
    <script language=\"javascript\">

function startCallback() {
    // make something useful before submit (onStart)
    // check recaptcha code

    var result = recaptchaGetValidation();
    document.getElementById('response').innerHTML = result

    if ( result == true ) {
        var form = document.forms['form_mail'];
        form.elements['recaptcha_validation'].value = '1';
    }
    else {
        var form = document.forms['form_mail'];
        form.elements['recaptcha_validation'].value = '0';
    }

    return true;



}

function completeCallback(response) {
    // make something useful after (onComplete)
    // document.getElementById('nr').innerHTML = parseInt(document.getElementById('nr').innerHTML) + 1;
    document.getElementById('response').innerHTML = response;

}
</script>

<form method='POST' name='form_mail' action='plugins/mailform.php' onsubmit='return AIM.submit(this, {onStart : startCallback, onComplete : completeCallback})'>
<table>";

    foreach ( $fields as $field ) {
        $desc = ucwords($field);
        $name = htmlentities($field);
        $form .= "<tr><td>$desc:</td><td><input type='text' name='$name'/></td></tr>";

    }

    $form .= "</table>
<input type='hidden' name='dest' value='$mail'/>
<input type='hidden' name='recaptcha_validation' value='0'/>
<br/>
"._("Please enter the two words in the box below").":<br/>
<div id=\"captchadiv\"></div>
<script type=\"text/javascript\" src=\"http://www.google.com/recaptcha/api/js/recaptcha_ajax.js\"></script>
<script type=\"text/javascript\">
    function showRecaptcha(element) {
        Recaptcha.create(\"6LeDYsASAAAAAOS-XKwyCrEik7ny-eV2aRCgGmM0\", element, {
        theme: \"white\",
        callback: Recaptcha.focus_response_field});
    }
    showRecaptcha('captchadiv');
</script>
<br/>



<input type='submit' name='submit_mail' value='"._("Send")."'/>
</form><div id='response'></div>";
    return $form;
}





// your form must include form_dest, form_mail and optionally form_return and form_subject


/*
$from_mail = $_POST["form_dest"];
$mail = $_POST["form_mail"];
$url = $_POST["form_url"];

foreach ($_POST as $a){
    if (!str_startswith($a, "form_")){
        $b = $_POST[$a];
        $message .= "$a = $b\n";
    }
}

if (isset($_POST["form_subject"])){
    $subject = $_POST["form_subject"];
} else {
    $subject = _("Message from PronaturaTours");
}

mail($mail, _("Message from PronaturaTours"), $message, $from_mail);
echo "succes?";

echo $message;
$link = rtrim(construct_link(array("page", "action")), "&");
echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=$url\">";
die();
*/
?>
